// service_post_order_default.dart
import 'package:casdon/common/state/common_net_status.dart';

class ServicePostOrder {
  final String expectArriveTime;
  final String orderNumber;
  final SalesChannel salesChannel;
  final int serviceCategory;
  final String address;
  final City city;
  final String contact;
  final County county;
  final String phone;
  final String memo;
  final Province province;
  final int type;
  final int source;
  final List<ProductLine> productLines;

  ServicePostOrder({
    required this.expectArriveTime,
    required this.orderNumber,
    required this.salesChannel,
    required this.serviceCategory,
    required this.address,
    required this.city,
    required this.contact,
    required this.county,
    required this.phone,
    required this.memo,
    required this.province,
    required this.type,
    required this.source,
    required this.productLines,
  });

  /// 默认初始化方法
  factory ServicePostOrder.initial() {
    return ServicePostOrder(
      expectArriveTime: '',
      orderNumber: '',
      salesChannel: SalesChannel.initial(),
      serviceCategory: 3, // 3: 安装， 4:维修
      address: '',
      city: City.initial(),
      contact: '',
      county: County.initial(),
      phone: '',
      memo: '',
      province: Province.initial(),
      type: 2, // 2:安装， 1: 维修
      source: 220,
      productLines: [ProductLine.initial()],
    );
  }

  ServicePostOrder copyWith({
    String? expectArriveTime,
    String? orderNumber,
    SalesChannel? salesChannel,
    int? serviceCategory,
    String? address,
    City? city,
    String? contact,
    County? county,
    String? phone,
    String? memo,
    Province? province,
    int? type,
    int? source,
    List<ProductLine>? productLines,
  }) {
    return ServicePostOrder(
      expectArriveTime: expectArriveTime ?? this.expectArriveTime,
      orderNumber: orderNumber ?? this.orderNumber,
      salesChannel: salesChannel ?? this.salesChannel,
      serviceCategory: serviceCategory ?? this.serviceCategory,
      address: address ?? this.address,
      city: city ?? this.city,
      contact: contact ?? this.contact,
      county: county ?? this.county,
      phone: phone ?? this.phone,
      memo: memo ?? this.memo,
      province: province ?? this.province,
      type: type ?? this.type,
      source: source ?? this.source,
      productLines: productLines ?? this.productLines,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'casdon_expectarrivetime': expectArriveTime,
      'casdon_order_number': orderNumber,
      'casdon_sales_channels_id': salesChannel.toJson(),
      'casdon_service_categories': serviceCategory,
      'new_address': address,
      'new_city_id': city.toJson(),
      'new_contact': contact,
      'new_county_id': county.toJson(),
      'new_feedbacktel': phone,
      'new_memo': memo,
      'new_province_id': province.toJson(),
      'new_type': type,
      'new_source': source,
      'lk_new_srv_workorder_new_srv_productline_new_workorder_id': productLines
          .map((e) => e.toJson())
          .toList(),
    };
  }
}

// ----------------- 默认初始化的子类 -----------------

class SalesChannel {
  final String name;
  final String logicalName;

  SalesChannel({required this.name, required this.logicalName});

  SalesChannel copyWith({String? name, String? logicalName}) {
    return SalesChannel(
      name: name ?? this.name,
      logicalName: logicalName ?? this.logicalName,
    );
  }

  factory SalesChannel.initial() =>
      SalesChannel(name: '', logicalName: 'casdon_sales_channels');

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'casdon_name': name},
      'logicalName': logicalName,
    };
  }
}

class City {
  final String code;
  final String name;
  final String logicalName;

  City({required this.code, required this.name, required this.logicalName});

  factory City.initial() => City(code: '', name: '', logicalName: 'new_city');

  City copyWith({String? code, String? name, String? logicalName}) {
    return City(
      code: code ?? this.code,
      name: name ?? this.name,
      logicalName: logicalName ?? this.logicalName,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'new_code': code, 'new_name': name},
      'logicalName': logicalName,
    };
  }
}

class County {
  final String code;
  final String name;
  final String logicalName;

  County({required this.code, required this.name, required this.logicalName});

  factory County.initial() =>
      County(code: '', name: '', logicalName: 'new_county');

  County copyWith({String? code, String? name, String? logicalName}) {
    return County(
      code: code ?? this.code,
      name: name ?? this.name,
      logicalName: logicalName ?? this.logicalName,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'new_code': code, 'new_name': name},
      'logicalName': logicalName,
    };
  }
}

class Province {
  final String code;
  final String name;
  final String logicalName;

  Province({required this.code, required this.name, required this.logicalName});

  Province copyWith({String? code, String? name, String? logicalName}) {
    return Province(
      code: code ?? this.code,
      name: name ?? this.name,
      logicalName: logicalName ?? this.logicalName,
    );
  }

  factory Province.initial() =>
      Province(code: '', name: '', logicalName: 'new_province');

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'new_code': code, 'new_name': name},
      'logicalName': logicalName,
    };
  }
}

class ProductLine {
  final String buyDate;
  final Product product;
  final ProductGroup productGroup;
  final ProductModel productModel;
  final int quantity;

  ProductLine({
    required this.buyDate,
    required this.product,
    required this.productGroup,
    required this.productModel,
    required this.quantity,
  });

  factory ProductLine.initial() => ProductLine(
    buyDate: '',
    product: Product.empty(),
    productGroup: ProductGroup.empty(),
    productModel: ProductModel.empty(),
    quantity: 1,
  );

  ProductLine copyWith({
    String? buyDate,
    Product? product,
    ProductGroup? productGroup,
    ProductModel? productModel,
    int? quantity,
  }) {
    return ProductLine(
      buyDate: buyDate ?? this.buyDate,
      product: product ?? this.product,
      productGroup: productGroup ?? this.productGroup,
      productModel: productModel ?? this.productModel,
      quantity: quantity ?? this.quantity,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'new_buydate': buyDate,
      'new_product_id': product.toJson(),
      'new_productgroup_id': productGroup.toJson(),
      'new_productmodel_id': productModel.toJson(),
      'new_qty': quantity,
    };
  }
}

class Product {
  final String name;
  final String productNumber;

  Product({required this.name, required this.productNumber});

  factory Product.empty() => Product(name: '', productNumber: '');

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'name': name, 'productnumber': productNumber},
      'logicalName': 'product',
    };
  }
}

class ProductGroup {
  final String name;

  ProductGroup({required this.name});

  factory ProductGroup.empty() => ProductGroup(name: '');

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'new_name': name},
      'logicalName': 'new_productgroup',
    };
  }
}

class ProductModel {
  final String name;

  ProductModel({required this.name});

  factory ProductModel.empty() => ProductModel(name: '');

  Map<String, dynamic> toJson() {
    return {
      'keyattributes': {'new_name': name},
      'logicalName': 'new_productmodel',
    };
  }
}
